package com.zhudi.develop_demo_as.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhudi.develop.util.ZhudiDebugLog;
import com.zhudi.develop.util.ZhudiScreenUtil;
import com.zhudi.develop_demo_as.BaseFragment;
import com.zhudi.develop_demo_as.MainApplication;
import com.zhudi.develop_demo_as.R;
import com.zhudi.develop_demo_as.ui.activity.DbActivity;
import com.zhudi.develop_demo_as.ui.activity.FlowActivity;
import com.zhudi.develop_demo_as.ui.activity.LoginActivity;
import com.zhudi.develop_demo_as.ui.activity.MaterialDialogActivity;
import com.zhudi.develop_demo_as.ui.activity.ShowPicContentActivity;

import butterknife.Bind;
import butterknife.OnClick;
import cn.lightsky.infiniteindicator.InfiniteIndicatorLayout;
import cn.lightsky.infiniteindicator.slideview.BaseSliderView;
import cn.lightsky.infiniteindicator.slideview.BaseSliderView.OnSliderClickListener;
import cn.lightsky.infiniteindicator.slideview.DefaultSliderView;

public class HomeFragment extends BaseFragment implements OnSliderClickListener{
	private int width;
	@Bind(R.id.tvTitle)
	TextView tvTitle;
	@Bind(R.id.sv)
	View sv;
	@Bind(R.id.ivPic)
	ImageView ivPic;
	@Bind(R.id.infinite_anim_circle)
	InfiniteIndicatorLayout mAnimCircleIndicator;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		ZhudiDebugLog.d("onAttach----->");
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ZhudiDebugLog.d("onCreate----->");
	}


	@Override
	protected int getLayoutView() {
		return R.layout.fragment_home;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		ZhudiDebugLog.d("onViewCreated----->");
		width = ZhudiScreenUtil.getScreenWidth(getActivity());
		initView(view);
		initAd(view);
	}

	private void initView(View view) {
		tvTitle.setText("首页");
		sv.setOverScrollMode(View.OVER_SCROLL_NEVER);

		Glide.with(getActivity())
				.load("http://cdn.duitang.com/uploads/item/201305/20/20130520115416_VrUUR.jpeg")
				.centerCrop()
				.thumbnail(0.1f)//缩略比例
				.placeholder(R.drawable.ic_photo_black_48dp)
				.error(R.drawable.ic_broken_image_black_48dp)
				.into(ivPic);
	}

	private void initAd(View view) {
		int[] pic = new int[]{R.drawable.ic_ad1,R.drawable.ic_ad2};
		ZhudiScreenUtil.setHeightNumber(mAnimCircleIndicator, width * 15 / 32);
		for (int i: pic) {
			DefaultSliderView textSliderView = new DefaultSliderView(getActivity());
			textSliderView.image(i)
					.setScaleType(BaseSliderView.ScaleType.Fit)
					.setOnSliderClickListener(this);
			textSliderView.getBundle().putString("extra","ceshi");
			mAnimCircleIndicator.addSlider(textSliderView);
		}
		//startAutoScroll() 启动轮循，间隔时间为getInterval().
		//startAutoScroll(int) 启动轮循，同时传入间隔时间
		//stopAutoScroll() 停止轮循
		//设置Indicator的显示位置
		mAnimCircleIndicator.setIndicatorPosition(InfiniteIndicatorLayout.IndicatorPosition.Center_Bottom);
		mAnimCircleIndicator.setScrollDurationFactor(5); //Page的滑动速度
		mAnimCircleIndicator.startAutoScroll(4);//启动轮循，同时传入间隔时间
	}

	@Override
	public void onSliderClick(BaseSliderView baseSliderView) {

	}

	@OnClick(R.id.btn)
	public void btnClick(){
		startActivity(new Intent(getActivity(), MaterialDialogActivity.class));
	}

	@OnClick(R.id.btnOne)
	public void btnOneClick(){
		startActivity(new Intent(getActivity(), ShowPicContentActivity.class));
	}

	@OnClick(R.id.btnTwo)
	public void btnTwoClick(){
		Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
		startActivityForResult(intent, 1);
	}

	@OnClick(R.id.btnThree)
	public void btnThreeClick(){
		startActivity(new Intent(getActivity(), LoginActivity.class));
	}

	@OnClick(R.id.btnFour)
	public void btnFourClick(){
		startActivity(new Intent(getActivity(), FlowActivity.class));
	}

	@OnClick(R.id.btnFive)
	public void btnDbClick(){
		startActivity(new Intent(getActivity(), DbActivity.class));
	}

	@Override
	public void onPause() {
		super.onPause();
		mAnimCircleIndicator.stopAutoScroll();
	}

	@Override
	public void onResume() {
		super.onResume();
		mAnimCircleIndicator.startAutoScroll();
	}


	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		ZhudiDebugLog.d("onActivityResult() requestCode:" + requestCode + ",resultCode:" + resultCode + ",data:" + data);
		if (null != data) {
			Uri uri = data.getData();
			if (uri == null) {
				return;
			} else {
				Cursor c = MainApplication.getInstance().getContentResolver().query(uri,
						new String[]{MediaStore.MediaColumns.DATA}, null,
						null, null);//根据返回的URI，查找数据库，获取视频的路径
				if (c != null && c.moveToFirst()) {
					String filPath = c.getString(0);
					ZhudiDebugLog.d(filPath);
					//new Upload(filPath).start();
				}
			}
		}
	}

//	selectedPhotos.clear();
//			if (photos != null) {
//				selectedPhotos.addAll(photos);
//			}
}
